package com.etc.controller.warning;

import com.etc.result.ResultBean;
import com.etc.service.impl.warning.BizThresholdServiceImpl;
import com.etc.entity.warning.BizThreshold;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @Main 预警阈值表的列清单控制层
 * @Author zsw
 * @Date 2019/12/3 18:39
 **/
@Controller
@RequestMapping("bizThreshold")
public class BizThresholdController {

    @Autowired
    private BizThresholdServiceImpl bizThresholdService;

    /**
     * 路径常量
     */
    private final String PATH = "warning/";


    /**
     * 根据预警等级查询
     * */
    @GetMapping("findByLevel/{level}")
    public List<BizThreshold> findByLevel(@PathVariable("level") String level){
        List<BizThreshold> bizThreshold2 = bizThresholdService.selectByLevel(level);
        return bizThreshold2;
    }

    /**
     * 根据位置查询查询
     * */
    @GetMapping("findBySensorPosition/{sensorPosition}")
    public List<BizThreshold> findBySensorPosition(@PathVariable("sensorPosition") String sensorPosition){
        List<BizThreshold> bizThreshold3 = bizThresholdService.selectBySensorPosition(sensorPosition);
        return bizThreshold3;
    }

    /**
     * 更新
     * */
    @PostMapping("update")
    @ResponseBody
    public ResultBean update(BizThreshold bizThreshold){

//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        bizThreshold.setUpdateDate(date);
        BizThreshold update = bizThresholdService.update(bizThreshold);
        return ResultBean.success(update);
    }

    /**
     * 根据id查询
     * */
    @GetMapping("findById/{id}")
    public String findById(@PathVariable("id") String id, Model model) {
        ResultBean bizThresholdServiceById = bizThresholdService.findById(id);
        model.addAttribute("threshold", bizThresholdServiceById.getData());
        return PATH + "threshold_update";
    }

    /**
     * 分页查询
     * */
    @GetMapping("threshold/{pageNum}/{type}")
    public String findByType(Model model,@PathVariable("type")String type,@PathVariable("pageNum") Integer pageNum,
                             BizThreshold bizThreshold){
        bizThreshold.setType(type);
        ResultBean criteria = bizThresholdService.findCriteriaByType(bizThreshold, pageNum, 20);
        model.addAttribute("list", criteria.getData());
        if (type.equals("1")){
            return PATH + "threshold_list";
        }else if (type.equals("2")){
            return PATH + "threshold_list_dynamic";
        }else if (type.equals("2")){
            return PATH + "threshold_list_static";
        }else if (type.equals("4") ){
            return PATH + "threshold_list_vibration";
        }else if (type.equals("5") ){
            return PATH + "threshold_list_soly";
        }else if (type.equals("6") ){
            return PATH + "threshold_list_expansion";
        }
        return null;
    }

    /**
     * 生成Excel
     * */
    @GetMapping("downExcel/{type}")
    public void downExcel(HttpServletResponse response,@PathVariable("type")String type) throws IOException {
        bizThresholdService.downExcel(type);
        File file = new File("E:\\waring_six.xls");
        FileInputStream fileInputStream = new FileInputStream(file);
        response.setHeader("content-disposition","attachment;fileName=BizThreshold.xls");
        ServletOutputStream outputStream = response.getOutputStream();
        FileCopyUtils.copy(fileInputStream,outputStream);
    }

    /**
     * 上传Excel
     * */
    @PostMapping("upExcel")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return "空文件";
        }
        String filename = file.getOriginalFilename();
        bizThresholdService.toExcel(filename,file);
        return "上传成功";
    }

}
